Ext.require([
    //'Ext.form.*',
    //'Ext.layout.container.Column',
    //'Ext.tab.Panel'
    '*'
]);

Ext.onReady(function() {
    Ext.QuickTips.init();

    var bd = Ext.getBody();
	
    Ext.apply(Ext.form.field.VTypes, {
        password: function(val, field) {
            if (field.initialPassField) {
                var pwd = field.up('form').down('#' + field.initialPassField);
                return (val == pwd.getValue());
            }
            return true;
        },

        passwordText: 'Passwords do not match'
    });	

    /*
     * ================  Simple form  =======================
     */
    
    var states = Ext.create('Ext.data.Store', {
        fields: ['abbr', 'name'],
        data : [
            {"abbr":"AL", "name":"您的生日"},
            {"abbr":"AK", "name":"您的第一所学校"},
            {"abbr":"AZ", "name":"您喜欢的歌曲"}
            //...
        ]
    });
    
    //bd.createChild({tag: 'h2', html: 'Form 1 - Very Simple'});

    var required = '<span style="color:red;font-weight:bold" data-qtip="Required">*</span>';

    var simple = Ext.widget({
        xtype: 'form',
        layout: 'form',
        collapsible: true,
        id: 'registerForm',
        url: 'register.action',
        frame: true,
        title: '用户注册',
        bodyPadding: '5 5 0',
        bodyStyle: {
            margin:'0 auto',
        },
        margin: '80px 500px 0px 480px',
        width: 350,
        fieldDefaults: {
            msgTarget: 'side',
            labelWidth: 75
        },
        defaultType: 'textfield',
        items: [{
            fieldLabel: '用户名',
            afterLabelTextTpl: required,
            value: 'fandy',
            name: 'user.username',
            allowBlank: false
        },{
            fieldLabel: '密码',
            afterLabelTextTpl: required,
            value: '111111',
            //name: 'user.pass',
			itemId: 'pass',
			inputType: 'password',
            allowBlank: false
        },{
            fieldLabel: '确认密码',
			afterLabelTextTpl: required,
			value: '111111',
            name: 'user.password',
            inputType: 'password',
            vtype: 'password',
            initialPassField: 'pass', // id of the initial password field
            allowBlank: false
        }, {
            fieldLabel: '电子邮件',
            afterLabelTextTpl: required,
            name: 'user.email',
            allowBlank: false,
            value: 'fanyy1984@163.com',
            vtype:'email'
        }, {
            xtype: 'radiogroup',
            afterLabelTextTpl: required,
            //name:'user.sex',
            fieldLabel: '性别',
            columns: 4,
            items: [
                {boxLabel: '男', name: 'user.sex', inputValue: true,checked: true},
                {boxLabel: '女', name: 'user.sex', inputValue: false, checked: false}
            ]
        },{
            fieldLabel: '密码问题',
            afterLabelTextTpl: required,
            xtype: 'combo',
            store: states,
            queryMode: 'local',
            name: 'user.pwdQuestion',
            displayField: 'name',
            valueField: 'abbr',
            value: '您的生日',
            allowBlank: false
        },{
            fieldLabel: '密码答案',
            afterLabelTextTpl: required,
            name: 'user.pwdAnswer',
            value: '19840913',
            allowBlank: false
        }
		],

        buttons: [{
            text: 'Save',
            handler: function() {
                this.up('form').getForm().isValid();
                this.up('form').getForm().submit({
                    clientValidation: true,
                    waitMsg: '信息提交中...',
                    success: function(form, action) {
                       Ext.Msg.alert('Success', action.result.msg,function(){
                    	   document.location.href="login!index.action";
                       });
                       
                    },
                    failure: function(form, action) {
                        switch (action.failureType) {
                            case Ext.form.action.Action.CLIENT_INVALID:
                                Ext.Msg.alert('Failure', 'Form fields may not be submitted with invalid values');
                                break;
                            case Ext.form.action.Action.CONNECT_FAILURE:
                                Ext.Msg.alert('Failure', 'Ajax communication failed');
                                break;
                            case Ext.form.action.Action.SERVER_INVALID:
                               Ext.Msg.alert('Failure', action.result.msg);
                       }
                    }
                });
            }
        },{
            text: 'Cancel',
            handler: function() {
                this.up('form').getForm().reset();
            }
        }]
    });

    simple.render(document.body);
});
